package com.eonmind.pay.mapper;

import com.eonmind.common.contract.data.base.BaseMongoMapper;
import com.eonmind.common.contract.timer.Timer;
import com.eonmind.common.provider.entity.pay.Order;
import com.eonmind.pay.dto.OrderDto;
import com.eonmind.pay.enums.TradeState;
import com.mongoplus.conditions.query.QueryWrapper;
import com.mongoplus.conditions.update.UpdateWrapper;
import com.mongoplus.model.PageResult;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @ClassName OrderMapper
 * @Description TODO
 * Author 32004
 * Date 2025/5/27 16:32
 * Version 1.0
 **/


@Component
public class OrderMapper extends BaseMongoMapper<Order> {
    public List<Order> selectNoPayOrder(Long userId, Integer type) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        // 过期前5分钟
        long startTime = Timer.currentTimestamp() - 300000;
        wrapper.eq(Order::getStatus, TradeState.NOTPAY.getCode());
        wrapper.eq(Order::getUserId, userId);
        wrapper.gte(Order::getCreateTime, startTime);
        wrapper.eq(Order::getType, type);
        return this.list(wrapper);
    }


    public void updateOrderStatusAndPayTime(long orderNo, Integer code, long payTime) {
        this.update(new UpdateWrapper<Order>()
                .eq(Order::getId, orderNo)
                .set(Order::getStatus, code)
                .set(Order::getPayTime, payTime)
                .set(Order::getUpdateTime, Timer.currentTimestamp())
        );
    }

    public PageResult<Order> pageOrder(Long id, OrderDto.OrderPageRequest pageRequest,List<Long> orderIds) {
        if(ObjectUtils.isEmpty(pageRequest)){
            return new PageResult<>();
        }
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq(Order::getUserId, id);
        wrapper.orderByDesc(Order::getUpdateTime);
        QueryWrapper<Order> wrapperParam = new QueryWrapper<>();
        boolean flag=false;
        if(pageRequest.getStatus()!=null){
           wrapperParam.or(new QueryWrapper<Order>().eq(Order::getStatus, pageRequest.getStatus()));
            flag=true;
        }
        if(pageRequest.getStartTime()!=null&&pageRequest.getEndTime()!=null){
            wrapperParam.and(new QueryWrapper<Order>().gte(Order::getCreateTime, pageRequest.getStartTime())
                    .lte(Order::getCreateTime, pageRequest.getEndTime()));
            flag=true;
        }
        if(pageRequest.getOrderNo()!=null){
            System.out.println(Long.parseLong(pageRequest.getOrderNo()));
            wrapperParam.or(new QueryWrapper<Order>().eq(Order::getId, Long.parseLong(pageRequest.getOrderNo())));
            flag=true;
        }
        if(!orderIds.isEmpty()){
            wrapperParam.or(new QueryWrapper<Order>().in(Order::getId, orderIds));
            flag=true;
        }
        if(flag){
            wrapper.and(wrapperParam);
        }


        return page(wrapper,pageRequest.getPage());
    }

    public List<Order> selectNoPayOrderByDuration(int milliseconds) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        long startTime = Timer.currentTimestamp() - milliseconds;
        wrapper.eq(Order::getStatus, TradeState.NOTPAY.getCode());
        wrapper.lte(Order::getUpdateTime, startTime);
        return this.list(wrapper);
    }

    public void setUpdateTime(Order order) {
        this.update(new UpdateWrapper<Order>()
                .eq(Order::getId, order.getId())
                .set(Order::getUpdateTime, Timer.currentTimestamp()));
    }

    public List<Order> getRecentOrder(Long id) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq(Order::getUserId, id);
        wrapper.orderByDesc(Order::getCreateTime);
        return this.list(wrapper);
    }
}
